package com.zx.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zx.reggie.common.BaseContext;
import com.zx.reggie.common.R;
import com.zx.reggie.pojo.AddressBook;
import com.zx.reggie.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 张旭
 * @version 1.0
 */
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    //新增 post
    @PostMapping
    public R<String> save(@RequestBody AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService.save(addressBook);
        return R.success("新建地址成功");
    }


    //设置默认地址 default  put
    /*
    * 先将该用户id下的所有地址更新为：非默认，即将is_default 都改为0
    * 再将指定的地址id的地址更新为：默认，即将该is_default 都改为1
    * */
    @PutMapping("/default")
    public R<AddressBook> setDefault(@RequestBody AddressBook addressBook) {
        Long userId = BaseContext.getCurrentId();
        //update address_book set is_default = 0 where user_id = ?
        LambdaUpdateWrapper<AddressBook> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AddressBook::getUserId, userId);
        updateWrapper.set(AddressBook::getIsDefault, 0);
        addressBookService.update(updateWrapper);

        //update address_book set is_default = 1 where id = ?
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);


        return R.success(addressBook);
    }

    @GetMapping("/default")
    public R<AddressBook> getDefault(){
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, currentId);
        queryWrapper.eq(AddressBook::getIsDefault,1);

        AddressBook one = addressBookService.getOne(queryWrapper);

        return R.success(one);
    }


    //数据回显 ——> 根据id查询 /{id}  get
    @GetMapping("/{id}")
    public R<AddressBook> getById(@PathVariable Long id) {

        AddressBook addressBook = addressBookService.getById(id);
        return R.success(addressBook);
    }

    //保存修改信息 put
    @PutMapping
    public R<String> updateById(@RequestBody AddressBook addressBook) {

        boolean b = addressBookService.updateById(addressBook);

        if (b) {
            return R.success("地址信息修改成功");
        }
        return R.error("未知错误");

    }

    //删除用户
    @DeleteMapping
    public R<String> deleteById(Long ids) {
        boolean b = addressBookService.removeById(ids);

        if (b) {
            return R.success("地址信息删除成功");
        }
        return R.error("未知错误");

    }


    //查询指定用户的全部地址 /list  get
    @GetMapping("/list")
    public R<List<AddressBook>> getAll() {

        LambdaQueryWrapper<AddressBook> addressBookLambdaQueryWrapper = new LambdaQueryWrapper<>();

        addressBookLambdaQueryWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        addressBookLambdaQueryWrapper.orderByDesc(AddressBook::getUpdateTime);

        List<AddressBook> list = addressBookService.list(addressBookLambdaQueryWrapper);

        return R.success(list);
    }
}
